package com.y.report.entity.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;
import java.util.Collections;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "R<T> 控制器响应model_统一出参", description = "统一出参")
public class R<T> implements Serializable {
    public static final long serialVersionUID = 0L;

    @Getter
    @ToString
    @AllArgsConstructor
    public enum Status {
        SUCCESS_CODE(20000), FAIL_CODE(50000);
        private final int code;

    }

    @Getter
    @ToString
    @AllArgsConstructor
    public enum Msg {
        OK("OK"), FAIL("FAIL");
        private final String msg;
    }


    @ApiModelProperty(value = "响应状态码_" + "响应[" + 20000 + "]为成功，其他编码为失败，如：[" + 50000 + "]", required = true)
    private int code;
    @ApiModelProperty(value = "响应状态_" + "响应消息，如：" + "[OK]" + "、" + "[FAIL]", required = true)
    private String status;
    @ApiModelProperty(value = "响应消息", required = true)
    private String message;
    @ApiModelProperty(value = "响应体_" + "响应数据体", required = true)
    private T content;
    @ApiModelProperty(value = "响应体_扩展消息_" + "其他消息", required = true)
    private Object extData;

    public static <IN> R<IN> success(IN content) {
        return of(Status.SUCCESS_CODE.getCode(), null, content);
    }

    public static R<Object> fail(String message) {
        return of(Status.FAIL_CODE.getCode(), message, Collections.emptyMap());
    }

    public static R<Object> ok() {
        return of(Status.SUCCESS_CODE.getCode(), null, Collections.emptyMap());
    }

    public static <IN> R<IN> of(int code, String message, IN content) {
        return new R<IN>(code, code == Status.SUCCESS_CODE.getCode() ? Msg.OK.getMsg() : Msg.FAIL.getMsg(), message, content, Collections.emptyMap());
    }
}
